home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8104 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  75 lines

  1. Path: news.duke.edu!acheron!philabs!usenet
  2. From: abf@philabs.research.philips.com (Andrew Feldman)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Class variables in C++
  5. Date: Wed, 14 Feb 1996 21:15:23 GMT
  6. Organization: Philips Laboratories, Briarcliff, NY 10510
  7. Distribution: inet
  8. Message-ID: <4ft8nn$hde@philabs.research.philips.com>
  9. References: <1996Feb14.165316@eigb>
  10. NNTP-Posting-Host: idrpc5.philabs.research.philips.com
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. On Wed, 14 Feb 1996 16:53:16 GMT, you wrote:
  14.  
  15. >Hi,
  16.  
  17. >Is it possible to create a class variable (or constant) in C++ ?
  18.  
  19. >For example:
  20.  
  21. >class ANIMAL
  22. >{
  23. >  protected:
  24. >    <I would like a class-variable (or constant), please>
  25. >    UINT uNumberOfLeg=0; /* pure ?!?! */
  26.  
  27. >  public:
  28. >    ...
  29. >};
  30.  
  31. >class CAT : public ANIMAL
  32. >{
  33. >  protected:
  34. >    <I would like a class-variable (or constant), please>
  35. >    UINT uNumberOfLeg=4;
  36.  
  37. >  public:
  38. >    ...
  39. >};
  40.  
  41. >Because I have never see a cat with 5 legs...
  42. >(I don't want to reserve 2 or more bytes in each instance of this class to
  43. >save the number of leg for a cat...)
  44.  
  45. >I hope this is comprehensive...
  46.  
  47. >Thanks
  48.  
  49. >                                               Laurent Guinnard
  50. >                                               guinnard@eig.unige.ch
  51.  
  52. >be_well++;
  53.  
  54. Use virtual functions.
  55.  
  56. class ANIMAL
  57. {
  58.    protected:
  59.      virtual UINT uNumberOfLeg() = 0; /* pure !!!! */
  60.  
  61.   public:
  62.     ...
  63. };
  64.  
  65. class CAT : public ANIMAL
  66. {
  67.    protected:
  68.       UINT uNumberOfLeg() { return 4; }
  69.    public:
  70.     ...
  71. };
  72.  
  73.  
  74.  
  75.